package com.course.dao;

import java.util.List;

import com.course.domain.MyClass;
import com.course.domain.Specialty;

public class MyClassDao extends SupperDao
{

	@SuppressWarnings( "unchecked" )
	public long countOfMyClass( int specialtyId )
	{
		String hql = "select count(*) from MyClass where specialty.id = " + specialtyId;
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< MyClass > getMyClasses( int start, int length, int specialtyId )
	{
		String hql = "from MyClass where specialty.id = " + specialtyId;
		return this.findPaging( hql, start, length, null );
	}

	public MyClass getMyClassById( int myClassId )
	{
		return ( MyClass )this.get( MyClass.class, myClassId );
	}
	
	public boolean saveMyClass( String name, String startYear, Integer lengthYear, Integer specialtyId )
	{
		MyClass myClass = new MyClass();
		myClass.setName( name );
		myClass.setFinish( false );
		myClass.setLengthYear( lengthYear );
		myClass.setStartYear( startYear );
		myClass.setSpecialty( ( Specialty )this.get( Specialty.class, specialtyId ) );
		return this.save( myClass );
	}

	public boolean updateMyClass( Integer id, String name, String startYear, Integer lengthYear, Boolean finish,  Integer specialtyId )
	{
		MyClass myClass = this.getMyClassById( id );
		myClass.setName( name );
		myClass.setFinish( finish );
		myClass.setLengthYear( lengthYear );
		myClass.setStartYear( startYear );
		myClass.setSpecialty( ( Specialty )this.get( Specialty.class, specialtyId ) );
		return this.update( myClass );
	}

	public boolean deleteMyClassById( Integer id )
	{
		MyClass myClass = ( MyClass )this.get( MyClass.class, id );
		return this.delete( myClass );
	}

	@SuppressWarnings( "unchecked" )
	public Specialty getSpecialtyByMyClassId( int myClassId )
	{
		List list = null;
		String hql = "select s from Specialty s, MyClass m where m.specialty.id = s.id and m.id = " + myClassId;
		list = ( List )this.findByHQL( hql );
		if(list.isEmpty())
			return null;
		return ( Specialty )list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< MyClass > getMyClassesByspecialtyId( int specialtyId )
	{
		String hql = "from MyClass m where m.specialty.id = " + specialtyId;
		return this.findByHQL( hql );
	}

	public boolean setFinish( int id )
	{
		MyClass myClass = ( MyClass )this.get( MyClass.class, id );
		myClass.setFinish( true );
		return this.update( myClass );
	}

	@SuppressWarnings( "unchecked" )
	public MyClass getMyClassByStudentId( int studentId )
	{
		List list = null;
		String hql = "select m from MyClass m, Student s where s.myClass.id = m.id and s.id = " + studentId;
		list = ( List )this.findByHQL( hql );
		if(list.isEmpty())
			return null;
		return ( MyClass )list.get( 0 );
	}
}
